#include "client.h"
#include "log.h"
#include "clientmenu.h"

// 启动客户端
int client_fd()
{
     // 创建套接字
     int client_fd = socket(AF_INET, SOCK_STREAM, 0);
     if (-1 == client_fd)
     {
          ERROR("创建套接字错误");
          exit(-1);
     }
     // 填充结构体
     struct sockaddr_in addr;
     addr.sin_addr.s_addr = stringIPtoNum(IP);
     addr.sin_port = htons(POST);
     addr.sin_family = AF_INET;
     // 链接
     while (-1 == connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)))
     {
          DEBUG("客户端等待连接中 \n");
          sleep(3);
     }
     DEBUG("客户端连接成功\n");

     //进入客户端的登录注册界面
     client_menu_login(client_fd);
     //发送

     //接收

     return 0;
}

// 把字符串IP转为数字IP
uint32_t stringIPtoNum(char *pIP)
{
     uint32_t nIP = 0;
     // AF_INET是代表IPV4地址的意思，AF_INET6代表IPV6
     inet_pton(AF_INET, pIP, &nIP);
     return nIP;
}

// 把数字IP转为字符串IP
void numIPtoString(uint32_t ip, char *psIP, int size)
{
     inet_ntop(AF_INET, &ip, psIP, size);
}

